<?php
/**
 * 广告管理
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: MyadController.class.php v1.0 2014-06-13 17:27:03 $
 */
namespace Admin\Controller;
class MyadController extends CommonController{
    private $model;
    private $service;
    public function _initialize() {
        parent::_initialize();
        $this->model = D("Myad");
        $this->service = D("Myad", "Service");
    }
    
    /**
     * 列表
     */
    public function index() {
        $result = $this->getPagination("Myad");
        //var_dump($result);die;
        $this->assign("list", $result['data']);
        $this->assign("count", $result['total_rows']);
        $this->assign("page", $result['show']);
        $this->display();
    }
    
    /**
     * 添加
     */
    public function add() {
        $this->display();
    }
    
    /**
     * 创建
     */
    public function create() {
        $myad = I("post.myad");
        $normbody = I("post.normbody");
        $adtype = I("post.adtype", NULL);
        
        if(!$adtype || !isArrayAndNotEmpty($myad)){
            return $this->errorReturn("无效操作！");
        }
        $link = addslashes($normbody['link']);
        switch ($adtype) {
            case "code":
                $myad['normbody'] = addslashes(htmlspecialchars_decode($normbody));
                break;
            case "image":
                if(empty($normbody['width'])){
                    $width = "";
                }else{
                    $width = " width=\"{$normbody['width']}\"";
                }
                if (empty($normbody['height'])){
                    $height = "";
                }else{
                    $height = "height=\"{$normbody['height']}\"";
                }
                $myad['normbody'] = "<a href=\"{$link}\" target=\"_blank\"><img src=\"{$normbody['url']}\"$width $height border=\"0\" /></a>";
                break;
            case "flash":
                if(empty($normbody['width'])) {
                    $width = "";
                } else {
                    $width = " width=\"{$normbody['width']}\"";
                }
                if (empty($normbody['height'])) {
                    $height = "";
                } else {
                    $height = "height=\"{$normbody['height']}\"";
                }
                $normbody = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.Macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\"$width $height><param name=\"movie\" value=\"{$link}\"/><param name=\"quality\" value=\"high\"/></object>";

                $myad['normbody'] = htmlspecialchars_decode($normbody);
                break;
            default:
                $myad['normbody'] = "<a href=\"{$link}\" font-size=\"{$normbody['size']}\" color=\"{$normbody['color']}\" target=\"_blank\">{$normbody['title']}</a>";
                break;
        }
        
        $result = $this->service->add($myad);
        if(!$result['status']){
            return $this->errorReturn($result['data']['error']);
        }
        
        return $this->successReturn("添加广告成功", U("index"));
    }
    
    /**
     * 修改
     */
    public function edit() {
        $aid = I("get.id", 0, "intval");
        if($aid <= 0){
            $this->error("无效操作！");
        }
        $myad = $this->service->getById($aid);
        $myad['normbody'] = stripslashes($myad['normbody']);
        if(!isArrayAndNotEmpty($myad)){
            $this->error("无效操作！");
        }
        $this->assign($myad);
        $this->display();
    }
    
    /**
     * 更新
     */
    public function update() {
        $myad = I("post.myad");
        if(isset($myad['tagname'])){
            unset($myad['tagname']);
        }
        $myad['normbody'] = addslashes(htmlspecialchars_decode($myad['normbody']));
        $result = $this->service->update($myad);
        if(!$result['status']){
            return $this->errorReturn($result['data']['error']);
        }
        
        return $this->successReturn("更新广告成功", U("index"));
    }
    
    public function delete() {
        $aid = I("post.id", 0, "intval");
        if($aid <= 0){
            $this->errorReturn("无效操作！");
        }
        
        if($this->service->delete($aid)){
            $this->successReturn("删除成功！");
        }else{
            $this->errorReturn("删除失败！");
        }
    }
}
